package com.voltmao.webpro.frame.view;

import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.FormViewResult;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhouyang on 2017/7/21.
 */
public abstract class ViewController implements IGridListView, IFromOrderView {
    /**
     * 表格视图页面
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/grid_list.json", method = {RequestMethod.GET, RequestMethod.POST})
    public GridViewResult gridLoadGridList(HttpServletRequest request) {

        GridViewResult builder = new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
//                String dataUrl=
                return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getRequestURI();
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {


                //返回查询数据
                return gridSearchPageData(request);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                //设置表格的默认数据


                return gridPageDefalutInfo(request);
            }
        });

        return builder;
    }

    /**
     * 列表页面跳转
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/from_info.json", method = {RequestMethod.GET, RequestMethod.POST})
    public FormViewResult fromInfo(HttpServletRequest request) {
        return new DTOSmith(request).builderFormViewResult(new DTOSmith.FormLoadBackCall() {
            @Override
            public Object resultFormData(HttpServletRequest request, FormViewResult result) throws WebProViewException {
                String modeType = request.getParameter("modeType");
                if ("view".equals(modeType)) {
                    //查询页面没有提交地址
                    result.requestUrl = "#";
                } else if ("edit".equals(modeType)) {
                    //编辑页面的提交地址
                    result.requestUrl = StringUtils.substringByLastChar(request.getRequestURL().toString(), "/") + "addOrUpdate.json";
                } else if ("add".equals(modeType)) {
                    result.requestUrl = StringUtils.substringByLastChar(request.getRequestURL().toString(), "/") + "addOrUpdate.json";
                    return null;
                }

                return formSearchByKeyId(request, modeType);
            }
        });
    }

    /**
     * 数据添加
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addOrUpdate.json", method = {RequestMethod.POST})
    public ApiResult addOrUpdate(HttpServletRequest request) {
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                String modeType = request.getParameter("modeType");
                return formInsertOrUpdate(request, modeType, apiResult);
            }
        });
    }

    /**
     * 数据添加
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/info_delete.json", method = {RequestMethod.POST})
    public ApiResult infoDelete(HttpServletRequest request) {
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                String modeType = request.getParameter("modeType");
                return apiDeleteKeys(request, modeType, apiResult);
            }
        });
    }


    /**
     * 查询表格数据
     *
     * @return
     */
    @Override
    public DataPage gridSearchPageData(HttpServletRequest request) throws WebProViewException {
        throw new WebProViewException("未实现数据查询接口");
    }

    /**
     * 默认数据
     *
     * @param request
     * @return
     * @throws WebProViewException
     */
    @Override
    public Object gridPageDefalutInfo(HttpServletRequest request) throws WebProViewException {
        throw new WebProViewException("未实现表格页面:设置表格页面默认的表单数据接口");
    }


    /**
     * 根据ID查询数据
     *
     * @param modeType 操作类型
     * @return
     * @throws WebProViewException
     */
    @Override
    public Object formSearchByKeyId(HttpServletRequest request, String modeType) throws WebProViewException {
        throw new WebProViewException("未实现接口方法.");
    }

    /**
     * 增加与修改事件
     *
     * @param request
     * @param apiResult
     * @return
     * @throws WebProViewException
     */
    @Override
    public Object formInsertOrUpdate(HttpServletRequest request, String modeType, ApiResult apiResult) throws WebProViewException {
        throw new WebProViewException("未实现接口方法.");
    }

    /**
     * 删除事件
     *
     * @param request
     * @return
     */
    @Override
    public Object apiDeleteKeys(HttpServletRequest request, String modeType, ApiResult apiResult) throws WebProViewException {
        throw new WebProViewException("未实现接口方法.");
    }
}
